package com.wanlian.router.ui.main;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.TextView;

import com.smart.android.ui.BaseFragment;
import com.smart.android.ui.bean.PageInfo;
import com.smart.android.ui.web.WebActivity;
import com.wanlian.router.R;
import com.wanlian.router.bean.Banner;
import com.wanlian.router.bean.Integral;
import com.wanlian.router.bean.Message;
import com.wanlian.router.bean.Page;
import com.wanlian.router.bean.ShareDisk;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;
import com.wanlian.router.ui.integral.IncomeDetailActivity;

import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/8/29.
 * Email: fvaryu@qq.com
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.recycler_view_point)
    RecyclerView mRecyclerViewPoint;

    @BindView(R.id.tv_integral)
    TextView mIntegral;
    @BindView(R.id.tv_notice)
    TextView mNotice;

    @BindView(R.id.tv_free)
    TextView mFree;
    @BindView(R.id.tv_picture)
    TextView mPicture;
    @BindView(R.id.tv_video)
    TextView mVideo;
    @BindView(R.id.tv_audio)
    TextView mAudio;

    private BannerAdapter mBannerAdapter;
    private PointAdapter mPointAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }


    @Override
    protected int layout() {
        return R.layout.fragment_home;
    }


    @Override
    protected void initUI(View view) {
        super.initUI(view);
        mRecyclerView.setLayoutManager(mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        mBannerAdapter = new BannerAdapter();
        mRecyclerView.setAdapter(mBannerAdapter);
        new PagerSnapHelper().attachToRecyclerView(mRecyclerView);

        mRecyclerViewPoint.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL ,false));
        mRecyclerViewPoint.setAdapter(mPointAdapter = new PointAdapter());

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    mPointAdapter.setCurrent(mLinearLayoutManager.findFirstVisibleItemPosition());
                }
            }
        });
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        NetWorkingClient.getInstance().queryBanner(getActivity(), null, new INetCallBack<List<Banner>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<Banner> banners) {
                if (response.isSuccess()) {
                    if (banners != null) {
                        mBannerAdapter.add(banners);
                        mPointAdapter.setSize(banners.size());
                    }
                }
            }
        });

        NetWorkingClient.getInstance().querySysNotice(getActivity(), new INetCallBack<Message>() {
            @Override
            public void onFinish(ResponseData response, @Nullable Message message) {
                if (response.isSuccess()) {
                    if (message != null) {
                        mNotice.setText(message.getTitle());
                        mNotice.setTag(message.getContent());
                        mNotice.setVisibility(View.VISIBLE);
                    }
                }
            }
        });



    }

    @Override
    public void onResume() {
        super.onResume();

        NetWorkingClient.getInstance().queryIntegralYesterday(getActivity(), new INetCallBack<Integral>() {
            @Override
            public void onFinish(ResponseData response, @Nullable Integral integral) {
                if (response.isSuccess()) {
                    double s = 0f;
                    String rmb = "";
                    if (integral != null) {
                       s = integral.getScore();
                       rmb = integral.getRmb();
                    }
                    String t = String.format(Locale.getDefault(),"%.6f ≈ %s元", s, rmb);
                    int index = t.indexOf(".");
                    SpannableString ss = new SpannableString(t);
                    ss.setSpan(new RelativeSizeSpan(0.5f), index, t.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mIntegral.setText(ss);
                }else {
                    SpannableString ss = new SpannableString("0 ≈ 0元");
                    ss.setSpan(new RelativeSizeSpan(0.5f), 2, ss.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mIntegral.setText(ss);
                }
            }
        });

        NetWorkingClient.getInstance().queryShareDiskSize(getActivity(), new INetCallBack<ShareDisk>() {
            @Override
            public void onFinish(ResponseData response, @Nullable ShareDisk shareDisk) {
                if (response.isSuccess()) {

                    if (shareDisk != null) {
                        mFree.setText(shareDisk.getUIFree());
                        mPicture.setText(shareDisk.getUIPhoto());
                        mAudio.setText(shareDisk.getUIMusic());
                        mVideo.setText(shareDisk.getUIMovie());
                    }else {
                        mFree.setText("0M");
                        mPicture.setText("0M");
                        mAudio.setText("0M");
                        mVideo.setText("0M");
                    }
                }
            }
        });
    }

    @OnClick({R.id.ll_income, R.id.tv_integral})
    void income() {
        redirectActivity(IncomeDetailActivity.class);
    }

    @OnClick(R.id.ll_yun_music)
    void music() {
        YunFilesActivity.go(getActivity(), 2);
    }


    @OnClick(R.id.ll_yun_picture)
    void picture() {
        YunFilesActivity.go(getActivity(), 0);
    }

    @OnClick(R.id.ll_yun_video)
    void video() {
        YunFilesActivity.go(getActivity(), 1);
    }

    @OnClick(R.id.tv_notice)
    void notice(View view) {
        if (view.getTag() == null) {
            return;
        }
        WebActivity.fire(getActivity(), (String) view.getTag());
    }
}
